{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def merge_vue_nvue_files(folder_path, output_file):\n",
    "    \"\"\"\n",
    "    将指定文件夹下所有的.vue和.nvue文件的内容合并到一个文件中。\n",
    "\n",
    "    :param folder_path: 要遍历的文件夹路径\n",
    "    :param output_file: 输出文件的路径\n",
    "    \"\"\"\n",
    "    # 确保输出文件路径存在\n",
    "    os.makedirs(os.path.dirname(output_file), exist_ok=True)\n",
    "    \n",
    "    with open(output_file, 'w', encoding='utf-8') as outfile:\n",
    "        # 遍历文件夹\n",
    "        for root, dirs, files in os.walk(folder_path):\n",
    "            for file in files:\n",
    "                # 检查文件扩展名\n",
    "                if file.endswith('.vue') or file.endswith('.nvue'):\n",
    "                    file_path = os.path.join(root, file)\n",
    "                    with open(file_path, 'r', encoding='utf-8') as infile:\n",
    "                        outfile.write(f\"// {file_path}\\n\")\n",
    "                        outfile.write(infile.read() + \"\\n\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# input /root/MyCode/trashbin/merge_prompt/code\n",
    "# output /root/MyCode/trashbin/merge_prompt/merge_output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "所有文件已合并\n"
     ]
    }
   ],
   "source": [
    "code_file_path = '/root/MyCode/trashbin/merge_prompt/input'\n",
    "output_prompt = '/root/MyCode/trashbin/merge_prompt/merge_output/prompt.txt'\n",
    "merge_vue_nvue_files(code_file_path, output_prompt)\n",
    "print(f\"所有文件已合并\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    folder_path = input(\"请输入文件夹路径: \")\n",
    "    output_file = input(\"请输入输出文件路径: \")\n",
    "    merge_vue_nvue_files(folder_path, output_file)\n",
    "    print(f\"所有文件已合并到 {output_file}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "science39",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
